<?php

$action = getAction();

switch ($action) {
	case "index":
		$sql = " select *  from ".table('setting');
		$smarty->assign("data", $db->getRow($sql));
		$smarty->display("regist_contract.html");
		break;
	case "agree":
		$smarty->display("regist_contract.html");
		break;
	case "add":
		$smarty->display("regist_add.html");
		break;
	case "addComp":
		$name = trim($_POST["name"]);
		$phone = trim($_POST["phone"]);
		$loginId = trim($_POST["loginId"]);
		$password = trim($_POST["password"]);
		$password2 = trim($_POST["password2"]);
		$result = array();
		
		//error check
		if(empty($name)) {
			$result["errmsg"] = "请输入姓名";
		} else if (empty($phone)) {
			$result["errmsg"] = "请输入手机号码";
		} else if (empty($loginId)) {
			$result["errmsg"] = "请输入用户名";
		} else if (empty($password)) {
			$result["errmsg"] = "请输入密码";
		} else if (strlen($password) < 6) {
			$result["errmsg"] = "请输入6位以上密码";
		} else if (empty($password2)) {
			$result["errmsg"] = "请输入确认密码";
		} else if ($password2  != $password) {
			$result["errmsg"] = "两次密码输入不相同";
		}
		
		if(!isset($result["errmsg"])) {
			//判断loginID是否重复
			$checkCnt = $db->getOne("select count(1) from " . table("broker") . " where login_id = '$loginId'");
			if($checkCnt) {
				$result["errmsg"] = "用户名已被注册";
			}
			//判断手机号码是否重复
			$checkCnt = $db->getOne("select count(1) from " . table("broker") . " where phone = '$phone'");
			if($checkCnt) {
				$result["errmsg"] = "手机号码已被注册";
			}
		}

		//注册入DB
		if(!isset($result["errmsg"])) {
			$data["name"] = $name;
			$data["phone"] = $phone;
			$data["login_id"] = $loginId;
			$data["password"] = md5($password);
			$data["isvalid"] = 1;
			$data["insert_time"] = getNowTime();
			$data["update_time"] = getNowTime();
			
			$db->insert("broker", $data);
			$result["succ"] = true;
		}
		
		header("content-type:text/plain;charset=utf-8");
		echo json_encode($result);
		exit();
		break;
}
?>